package hn.cch.mybatis.service.impl;

import hn.cch.mybatis.entity.TableEntity;
import hn.cch.mybatis.mapper.MyBatisMapper;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyBatisServiceImpl {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    public void testTemplate(){

        // Object selectOne = sqlSessionTemplate.selectOne();
    }

    public void testSession(){
        List<TableEntity> tableEntityList = new ArrayList<>();
        tableEntityList.add(new TableEntity());
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        try {
            MyBatisMapper myBatisMapper = sqlSession.getMapper(MyBatisMapper.class);
            for (TableEntity tableEntity: tableEntityList){
                int rows = myBatisMapper.insertTable(tableEntity);
            }
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
    }



}
